"""
清洗全球疫情数据，提供接口，数据文件为data/cov_info_of_world.json
"""

import pandas as pd


class GlobalData:
    def __init__(self):
        self.origin_data = pd.DataFrame()  # 文件中的原始数据
        self.names_of_countries = []  # 国家名称
        self.confirmed_count_of_countries = []  # 各国确诊人数
        self.current_confirm_count_of_countries = []  # 各国现存确诊人数
        self.death_rate_of_countries = []  # 各国死亡率
        self.format_data_of_countries()  # 清洗数据

    # 格式化数据
    def format_data_of_countries(self):
        self.open_data_file()
        self.format_name_of_countries()
        self.format_confirm_count_of_countries()
        self.format_current_confirm_count_of_countries()
        self.format_death_rate_of_countries()

    # 读取文件原始数据，保存至origin_data中
    def open_data_file(self):
        self.origin_data = pd.read_json('./data/cov_info_of_world.json')[
            ['provinceName', 'countryFullName', 'confirmedCount', 'currentConfirmedCount', 'deadRate']]
        self.origin_data.dropna(inplace=True)
        # 请洗无效数据
        self.origin_data.drop(self.origin_data[(self.origin_data['currentConfirmedCount'] < 0) | (
                self.origin_data['confirmedCount'] < 0) | (self.origin_data['deadRate'] < 0) |
                                               (self.origin_data['deadRate'] > 100)].index, inplace=True)

    # 顺序读取origin_data中的国家全名，countryFullName，保存至names_of_countries
    def format_name_of_countries(self):
        self.names_of_countries = self.origin_data['countryFullName'].to_list()
        # print(self.names_of_countries)

    # 顺序读取origin_data中的国家确诊人数，confirmedCount，保存至confirmed_count_of_countries

    def format_confirm_count_of_countries(self):
        self.confirmed_count_of_countries = self.origin_data['confirmedCount'].to_list(
        )
        # print(self.confirmed_count_of_countries)

    # 顺序读取origin_data中的国家现存确诊人数，currentConfirmedCount，保存至current_confirm_count_of_countries
    def format_current_confirm_count_of_countries(self):
        self.current_confirm_count_of_countries = self.origin_data['currentConfirmedCount'].to_list(
        )
        # print(self.current_confirm_count_of_countries)

    # 顺序读取origin_data中的国家死亡率，deadRate，保存至death_rate_of_countries
    def format_death_rate_of_countries(self):
        self.death_rate_of_countries = self.origin_data['deadRate'].to_list()
        # print(self.death_rate_of_countries)


if __name__ == '__main__':
    sy = GlobalData()
